package com.bjsc.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bjsc.entity.model.InAndOut;
import com.bjsc.entity.model.Player;
import com.bjsc.mapper.UserMapper;
import com.bjsc.redis.RedisAdapter;
import com.bjsc.service.IPlayerService;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.bjsc.entity.model.BetRecord;
import com.bjsc.enums.BooleanEnum;
import com.bjsc.mapper.PlayerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author create by VIC.Huang
 * @date 2018/2/25
 */
@Service
public class PlayerServiceImpl implements IPlayerService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisAdapter redisAdapter;

    @Autowired
    private PlayerMapper playerMapper;

    @Override
    public Page<Map<Object, Object>> selectPlayerList(Page<Map<Object, Object>> page, String search) {
        page.setRecords(playerMapper.selectPlayerList(page, search));
        return page;
    }

    @Override
    public Page<Map<Object, Object>> selectBetRecords(Page<Map<Object, Object>> page, Long playerId) {
        List<BetRecord> tempList = playerMapper.selectBetRecords(page, playerId);
        List<Map<Object, Object>> result = new ArrayList<>(tempList.size());
        ObjectMapper objectMapper = new ObjectMapper();
        for (BetRecord temp : tempList) {
            result.add(objectMapper.convertValue(temp, Map.class));
        }
        page.setRecords(result);
        return page;
    }

    @Override
    public Page<Map<Object, Object>> selectWithdrawRecords(Page<Map<Object, Object>> page, Long playerId) {
        page.setRecords(playerMapper.selectWithdrawRecords(page, playerId));
        return page;
    }

    @Override
    public Page<Map<Object, Object>> selectRechargeRecords(Page<Map<Object, Object>> page, Long playerId) {
        page.setRecords(playerMapper.selectRechargeRecords(page, playerId));
        return page;
    }

    private static String CACHE_TOKEN_PREFIX = "token:";
    @Override
    public boolean frozenPlayer(Long id) {
        Set<String> tokenKeys = redisAdapter.keys(CACHE_TOKEN_PREFIX + "*");
        for(String tokenKey : tokenKeys) {
            Long idCache = Long.parseLong(redisAdapter.get(tokenKey));
            if(idCache.compareTo(id) == 0) {
                redisAdapter.del(tokenKey);
                break;
            }
        }
        return userMapper.frozenUser(id) > 0;
    }

    @Override
    public boolean unfrozenPlayer(Long id) {
        return userMapper.unfrozenUser(id) > 0;
    }

    @Override
    public InAndOut getInAndOutByPlayerId(Long playerId) {
        // 获取总充值
        InAndOut inAndOut = new InAndOut();
        BigDecimal totalRecharge = playerMapper.sumRecharge(playerId);
        BigDecimal totalWithdraw = playerMapper.sumWithdraw(playerId);
        BigDecimal totalCommission = playerMapper.sumCommission(playerId);
        BigDecimal totalPayback = playerMapper.sumPayback(playerId);
        BigDecimal userAsset = playerMapper.getUserAsset(playerId);

        inAndOut.setTotalCommission(totalCommission == null ? BigDecimal.ZERO : totalCommission)
                .setTotalPayback(totalPayback == null ? BigDecimal.ZERO : totalPayback)
                .setTotalRecharge(totalRecharge == null ? BigDecimal.ZERO : totalRecharge)
                .setTotalWithdraw(totalWithdraw == null ? BigDecimal.ZERO : totalWithdraw)
                .setTotalWin((inAndOut.getTotalWithdraw().add(userAsset == null ? BigDecimal.ZERO : userAsset)).subtract(inAndOut.getTotalRecharge()));
        return inAndOut;
    }

    @Override
    public Page<Map<Object, Object>> selectInvitePlayerList(Page<Map<Object, Object>> page, Long playerId) {
        page.setRecords(playerMapper.selectInvitePlayers(page, playerId));
        return page;
    }

    @Override
    public boolean frozenAllPlayer() {
        Set<String> tokenKeys = redisAdapter.keys(CACHE_TOKEN_PREFIX + "*");
        redisAdapter.del(tokenKeys.toArray(new String[]{}));
        return userMapper.frozenAllUser() > 0;
    }

    @Override
    public boolean setToSystemPlayer(Long id) {
        return userMapper.updateUserSystemFlag(id, BooleanEnum.YES.getCode()) > 0;
    }

    @Override
    public boolean setToNormalPlayer(Long id) {
        return userMapper.updateUserSystemFlag(id, BooleanEnum.NO.getCode()) > 0;
    }
}
